package com.pks.datacenter.controller.user;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.swagger.annotations.Api;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.pks.datacenter.aspect.AspectLogTag;
import com.pks.datacenter.common.WebContext;
import com.pks.datacenter.config.MyConfig;
import com.pks.datacenter.config.RedisKeyConfig;
import com.pks.datacenter.dto.YunUserDto;
import com.pks.datacenter.entity.user.User;
import com.pks.datacenter.handler.RedisHandler;
import com.pks.datacenter.handler.YunHandler;
import com.pks.datacenter.service.user.UserService;
import com.pks.resultful.common.SRemoteResult;
import com.pks.util.UtilString;
import com.pks.web.springmvc.controller.BasicController;


@RestController
@RequestMapping(value = "/user")
@Scope("prototype")
@Component("userController")
@Api(tags = "用户")
public class UserController extends BasicController {

	@Resource
	private UserService userService;
	@Resource
	private RedisHandler redisHandler;
	@Resource
	private YunHandler yunHandler;

	@Value("${spring.profiles.active}")
	private String springProfilesActive;
	

	@RequestMapping(value = "/login" )
	public SRemoteResult<YunUserDto> findInfo(HttpServletRequest request,@RequestParam(required = true) String account,@RequestParam(required = true) String password) {
		if (!"dev".equals(springProfilesActive)) { 
			return SRemoteResult.initError("仅供测试环境");
		}
		if (StringUtils.isBlank(account) || StringUtils.isBlank(password)) {
			return SRemoteResult.initErrorParamMissing(null);
		}
		Map<String, Object> qbuilder = new HashMap<String, Object>();
		qbuilder.put("account", account.trim());
		qbuilder.put("password", password.trim());
		List<User> list = userService.find(null, qbuilder, 1);
		if (list.size() == 0) {
			return SRemoteResult.initError("用户名或密码错误");
		}
		User user = list.get(0);
		user.setPassword(null);
		user.setMobile(null);
		
		YunUserDto userDto = new YunUserDto();
		userDto.setRealName(user.getUserName());
		userDto.setShow_token(UtilString.getUUID());
		userDto.setShow_loginip(WebContext.getRealIpAddress(request));
		WebContext.updateLoginUser(request, userDto);
		
		return SRemoteResult.initSuccess(userDto);
	}
	
	@RequestMapping(value = "/loginByToken")
	@AspectLogTag(operType = "登录")
	public SRemoteResult<YunUserDto> loginByToken(HttpServletRequest request, String token,Long mid) {
		if (StringUtils.isBlank(token)) {
			return SRemoteResult.initErrorParamBlank("token");
		}
		if (mid != null) {
			if (!mid.equals(MyConfig.moduleId)) {
				MyConfig.moduleId = mid;
			}
		}
		SRemoteResult<YunUserDto> sr_login = yunHandler.findUserByToken(token);
		if (!sr_login.success()) {
			return sr_login;
		}
		YunUserDto user = sr_login.getD();
		if (user != null) {
			user.setShow_token(UtilString.getUUID());
			user.setShow_loginip(WebContext.getRealIpAddress(request));
			WebContext.updateLoginUser(request, user);
			redisHandler.set(RedisKeyConfig.KEY_LOGIN_TOKEN + user.getShow_token(), user, 2 * 3600);
			return SRemoteResult.initSuccess(user);
		}
		return SRemoteResult.initError("登录失败");
	}
	
	@AspectLogTag(operType = "退出登录")
	@RequestMapping(value = "/logout")
	public SRemoteResult<String> logout(HttpServletRequest request) {
		YunUserDto loginUser = WebContext.getLoginUser(request);
		if (loginUser != null) {
			WebContext.removeLoginUserAndSession(request);
			redisHandler.del(RedisKeyConfig.KEY_LOGIN_TOKEN + loginUser.getShow_token());
		}
		return SRemoteResult.initSuccess(null);
	}

	@RequestMapping(value = "/getLoginUserInfo")
	public SRemoteResult<YunUserDto> getLoginUserInfo(HttpServletRequest request) {
		YunUserDto loginUser = WebContext.getLoginUser(request);
		return SRemoteResult.initSuccess(loginUser);
	}

}
